<?
//查询满足条件的买入股票
$sqlbuy="select * from {$db_prefix}gp_market where username!='".$saleouthy."' and num>0 and type=1 and state=0 and price>='$price' order by id asc";
$resultbuy=$db->query($sqlbuy);
while($rsbuy=$db->fetch_array($resultbuy)){
	//查询出交易数量
	if(intval($num)>0){
	if (intval($num)>$rsbuy['num']){
		//更新买入股票的数量和状态
		$sqlgx="update {$db_prefix}gp_market set num=0,state=1,cjtime='$curtime' where id='".$rsbuy['id']."'";
		$db->query($sqlgx);
		$curtradeallnum+=$rsbuy['num'];
		//更新售出股票的数量
		$sqlgx="update {$db_prefix}gp_market set num=num-'".$rsbuy['num']."' where id='".$curmarketid."'";
		$db->query($sqlgx);
		//插入交易记录中
		$sqljy="insert into {$db_prefix}gp_trade(username,username1,saleid,buyid,type,addtime,price,num,tradeprice) values('".$saleouthy."','".$rsbuy['username']."','".$curmarketid."','".$rsbuy['id']."',2,'".$curtime."','".$price."','".$rsbuy['num']."','$gptradeprice')";
		//echo $sqljy."<Br>";
		$db->query($sqljy);
		//更新卖出股票人的电子股
		$saleprice=$price*$rsbuy['num'];
		
		//推荐人获取交易额的5代的1%
		if ($saleprice>0){
			$sqlhy1="select id,tjuser from {$db_prefix}users where username='".$saleouthy."'";
			$rshy1=$db->get_one($sqlhy1);
			if ($rshy1['tjuser']){
				$hytjupstr='';
				gettjupstrfunc($rshy1['tjuser'],1,$glo_gpdai);
				if ($hytjupstr!=''){
					$hytjary=explode(',',$hytjupstr);
					foreach($hytjary as $gpk1=>$gpv1){
						$yxbprice=0;
						$yxbprice=$saleprice*$glo_gprate/100;
						$sqlq1="update {$db_prefix}users set priceall=priceall+'$yxbprice',jjprice=jjprice+'$yxbprice' where id='".$gpv1."'";
						$db->query($sqlq1);
						$e_userid=$gpv1;$e_price=$yxbprice;$e_type=19;$e_ptype=3;$e_addtime=$curtime;$e_memo="报单币提成";
						hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
					}
				}
			}
		}
		
		//交易收取费用
		$salepricefy=$saleprice*($glo_gp_traderate+$glo_gpjjrate)/100;
		$salepricesj=$saleprice*(1-($glo_gp_traderate+$glo_gpjjrate)/100);
		
		//进入慈善基金
		/*$csjjprice=$saleprice*$glo_gpjjrate/100;
		$sqlcsjj="update {$db_prefix}salaryset set csjjprice=csjjprice+'$csjjprice' where 1";
		$db->query($sqlcsjj);*/
		
		//30%进入电子股 70%进入电子钱包
		$salepricesj1=$salepricesj*$glo_gp_pricerate/100;
		$salepricesj2=$salepricesj*$glo_gp_pricerate1/100;
		
		//增加会员的电子股金额
		$sqlq="update {$db_prefix}users set gpprice=gpprice+'$salepricesj1',jjprice=jjprice+'$salepricesj2' where username='".$saleouthy."'";
		$db->query($sqlq);
		//插入到电子政务记录中 注册人的电子政务
		if($salepricesj1>0){
			//财务记录
			$e_userid=$saleouthyid;$e_price=$salepricesj1;$e_type=2;$e_ptype=4;$e_addtime=$curtime;$e_memo="售出报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		if($salepricesj2>0){
			//财务记录
			$e_userid=$saleouthyid;$e_price=$salepricesj2;$e_type=17;$e_ptype=3;$e_addtime=$curtime;$e_memo="售出报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		//更新买入股票的数量
		$sqlq1="update {$db_prefix}users set gpnum=gpnum+'".$rsbuy['num']."' where username='".$rsbuy['username']."'";
		$db->query($sqlq1);
		//更新剩余的数量
		$num=intval($num)-$rsbuy['num'];
	}else{
		//更新买入股票的数量
		$curtradeallnum+=intval($num);
		$sqlgx="update {$db_prefix}gp_market set num=num-'".intval($num)."' where id='".$rsbuy['id']."'";
		$db->query($sqlgx);
		if (intval($num)==$rsbuy['num']){
			$sqlgx="update {$db_prefix}gp_market set state=1,cjtime='$curtime' where id='".$rsbuy['id']."'";
			$db->query($sqlgx);
		}
		//更新售出股票的数量和状态
		$sqlgx="update {$db_prefix}gp_market set num=0,state=1,cjtime='$curtime' where id='".$curmarketid."'";
		$db->query($sqlgx);
		//插入交易记录中
		$sqljy="insert into {$db_prefix}gp_trade(username,username1,saleid,buyid,type,addtime,price,num,tradeprice) values('".$saleouthy."','".$rsbuy['username']."','".$curmarketid."','".$rsbuy['id']."',2,'".$curtime."','".$price."','".intval($num)."','$gptradeprice')";
		$db->query($sqljy);
		//更新卖出股票人的电子股
		$saleprice=$price*intval($num);
		
		//推荐人获取交易额的5代的1%
		if ($saleprice>0){
			$sqlhy1="select id,tjuser from {$db_prefix}users where username='".$saleouthy."'";
			$rshy1=$db->get_one($sqlhy1);
			if ($rshy1['tjuser']){
				$hytjupstr='';
				gettjupstrfunc($rshy1['tjuser'],1,$glo_gpdai);
				if ($hytjupstr!=''){
					$hytjary=explode(',',$hytjupstr);
					foreach($hytjary as $gpk1=>$gpv1){
						$yxbprice=0;
						$yxbprice=$saleprice*$glo_gprate/100;
						$sqlq1="update {$db_prefix}users set priceall=priceall+'$yxbprice',jjprice=jjprice+'$yxbprice' where id='".$gpv1."'";
						$db->query($sqlq1);
						$e_userid=$gpv1;$e_price=$yxbprice;$e_type=19;$e_ptype=3;$e_addtime=$curtime;$e_memo="报单币提成";
						hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
					}
				}
			}
		}
		
		//交易收取费用
		$salepricefy=$saleprice*($glo_gp_traderate+$glo_gpjjrate)/100;
		$salepricesj=$saleprice*(1-($glo_gp_traderate+$glo_gpjjrate)/100);
		
		//进入慈善基金
		/*$csjjprice=$saleprice*$glo_gpjjrate/100;
		$sqlcsjj="update {$db_prefix}salaryset set csjjprice=csjjprice+'$csjjprice' where 1";
		$db->query($sqlcsjj);*/
		
		//30%进入电子股 70%进入电子钱包
		$salepricesj1=$salepricesj*$glo_gp_pricerate/100;
		$salepricesj2=$salepricesj*$glo_gp_pricerate1/100;
		
		//增加会员的电子股金额
		$sqlq="update {$db_prefix}users set gpprice=gpprice+'$salepricesj1',jjprice=jjprice+'$salepricesj2' where username='".$saleouthy."'";
		$db->query($sqlq);
		//插入到电子政务记录中 注册人的电子政务
		if($salepricesj1>0){
			//财务记录
			$e_userid=$saleouthyid;$e_price=$salepricesj1;$e_type=2;$e_ptype=4;$e_addtime=$curtime;$e_memo="售出报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		if($salepricesj2>0){
			//财务记录
			$e_userid=$saleouthyid;$e_price=$salepricesj2;$e_type=17;$e_ptype=3;$e_addtime=$curtime;$e_memo="售出报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		//更新买入股票的数量
		$sqlq1="update {$db_prefix}users set gpnum=gpnum+'".intval($num)."' where username='".$rsbuy['username']."'";
		$db->query($sqlq1);
		//更新剩余的数量
		$num=0;
	}
	}
}
$db->free_result($resultbuy);


?>